package cz.mendelu.swi2.skladDilu;

import java.util.Date;
import java.util.Map;



/**
 * Inventura je prostredek pro vyrovnani fyzickych stavu dilu na sklade se zaznamy
 * v systemu.
 * @author infsys
 * @version 1.0
 * @created 20-4-2012 12:47:50
 */
public class Inventory {

	private Date date;
	private Part[] result;
	private User user;
	private Warehouse warehouse;

	public Inventory(Warehouse warehouse){
        this.warehouse=warehouse;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Part[] getResult() {
		return result;
	}

	public void setResult(Part[] result) {
		this.result = result;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Warehouse getWarehouse() {
		return warehouse;
	}

	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	/*
	 * 
	 */
	private void countDiff(){

		for (Part part : warehouse.getListOfParts()) {
			part.setInventorydiff(part.getInventoryState()-part.getCount());
		}
	}

	/*
	 * 
	 */
	public boolean processInventory(Map<String, String[]> parameters) {
		for(String parameter : parameters.keySet()) 
		{
	        String[] values = parameters.get(parameter);
	        if(isValueValid(values[0]))
	        	warehouse.updateInventoryStateOnProdukt(Long.valueOf(parameter), Integer.valueOf(values[0]));
	        else return false;
		}
		countDiff();
		return true;
	}

	private boolean isValueValid(String string) {
		try
		{
			Integer.valueOf(string);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}

}